package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import ve.com.wayoyo.wuml_case.Painter;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.FloatMath;

public class CompLifeBar extends Componente{

	final public static int INITIAL_HEIGHT = 150;
	final public static int BAR_WIDHT = 30;
	final static int SizeSteeps = 20;
	final static int MinLifeBarSize = 40;
	
	private int MaxBoundY;
	private int MinBoundY;
	
	public CompLifeBar(){
		_height = INITIAL_HEIGHT;
		_width = BAR_WIDHT;
		_centerPoint = new Point(0,0);
		Anchor = new Point(0,0);
		_connectionPoints = new Point[16];
		for (int i = 0; i < _connectionPoints.length; i++) {
			_connectionPoints[i] = new Point();
		}
	}
	
	@Override
	public void setTitulo(String titulo) {
		
		
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
		int offSetX, int offSetY) {
		int posX = _centerPoint.x + offSetX;
		int posY = _centerPoint.y + offSetY;
		RectF borde = new RectF();
		borde.set((posX - (_width / 2)) * globalScale, (posY - (_height / 2))
				* globalScale, (posX + (_width / 2)) * globalScale,
				(posY + (_height / 2)) * globalScale);
		c.drawRect(borde, Painter.getInstancia().getBlockPaint(VOID_STATE, true));
		c.drawRect(borde, Painter.getInstancia().getBlockPaint(_currentState, false));
		
		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}
		
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		return true;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
			}
		}
		return respuesta;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		int cpDist;
		cpDist = (int) _height / 9;
		for (int i = 0; i < 8; i++) {
			_connectionPoints[i].set(Anchor.x,
					(int) (Anchor.y + ((i + 1) * cpDist)));
		}
		for (int i = 8; i < 16; i++) {
			_connectionPoints[i].set((int) (Anchor.x + _width),
					(int) (Anchor.y + ((i - 7) * cpDist)));
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsBegining) {
				value.CalcularConexiones(this, VistaDiagrama.getVista()
						.getDiagrama().getComponenteById(value._childId));
			}
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsEnd) {
				value.CalcularConexiones(VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(value._fatherId), this);
			}
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		_centerPoint.x = (int)(Anchor.x + (_width/2));
		_centerPoint.y = (int)(Anchor.y + (_height/2));
	}

	@Override
	public String generarDOM() {
		String DOM = "\t\t<SubComponente Tipo=\"LifeBar\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y +"\" Height=\"" + Integer.toString((int)_height)+"\">\n";
		DOM = DOM + "\t\t</SubComponente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public String getTipo() {
		return "LifeBar";
	}
	
	public void setInitialPos(int PosX,int PosY){
		_centerPoint.set(PosX, (int) (PosY+(_height/2)));
		Anchor.x = (int) (PosX-(_width/2));
		Anchor.y = (int) (PosY);
		actualizarAchorsPoints();
	}
	
	@Override
	public void mover(int distX, int distY) {
		//Anchor.x += distX;
		Anchor.y += distY;
		if(Anchor.y < MinBoundY){
			Anchor.y = MinBoundY;
		}else if(Anchor.y+_height > MaxBoundY){
			Anchor.y = (int) (MaxBoundY - _height);
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}
	
	public void moverTodo(int distX, int distY){
		Anchor.x += distX;
		Anchor.y += distY;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}
	
	public void setBounds(int min,int max){
		MaxBoundY = max;
		MinBoundY = min;
		mover(0, 0); //Hacemos una llamada a la funcion de mover para actulizar la posicion de barra en caso de que ya no este en los bounds
	}
	
	public void align(int posX){
		_centerPoint.x = posX;
		Anchor.x = (int) (_centerPoint.x-(_width/2));
	}
	
	public void aumentar(){
		_height += SizeSteeps;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}
	
	public void reducir(){
		_height -= SizeSteeps;
		if(_height <= MinLifeBarSize){
			_height = MinLifeBarSize;
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}
	
	@Override
	public Point requestConnectionPoint(CompConector connector, int mode,
			Point otherCompCenter) {
		float distMin = Float.MAX_VALUE;
		float dX = 0;
		float dY = 0;
		float disActual;
		int CP_MasCercano = 0; // Punto de conexion con menor distancia
		for (int i = 0; i < _connectionPoints.length; i++) {
			dX = _connectionPoints[i].x - otherCompCenter.x;
			dY = _connectionPoints[i].y - otherCompCenter.y;
			disActual = FloatMath.sqrt(dX * dX + dY * dY);
			if (disActual < distMin) {
				distMin = disActual;
				CP_MasCercano = i;
			}
		}
		return _connectionPoints[CP_MasCercano];
	}
}
